package com.myzl.coal.mq;

import com.myzl.coal.domain.AlarmEvent;
import com.myzl.coal.domain.dto.RealTimeDataDTO;
import com.myzl.coal.dto.CmdCallBackDTO;
import com.myzl.coal.dto.CommandDTO;
import com.myzl.coal.dto.DeviceStatusDTO;
import com.myzl.coal.dto.SendCmdDTO;
import com.myzl.common.enums.DeviceStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.myzl.coal.mq.MqConstant.*;

/**
 * @ClassName MqService
 * @Author wzb
 * @Date 2022/7/19 14:26
 * @Description
 */
@Service
@Slf4j
public class MqService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendCmdFail(SendCmdDTO sendCmdDTO) {
        CmdCallBackDTO cmdCallBackDTO = new CmdCallBackDTO();
        BeanUtils.copyProperties(sendCmdDTO, cmdCallBackDTO);
        cmdCallBackDTO.setStatus(false);
        rabbitTemplate.convertAndSend("cmdCallBack", null, cmdCallBackDTO);

        removeCmd(sendCmdDTO);
    }


    public void sendCmdSuccess(SendCmdDTO sendCmdDTO) {
        if (Objects.nonNull(sendCmdDTO) && sendCmdDTO.getCallBack()) {
            CmdCallBackDTO cmdCallBackDTO = new CmdCallBackDTO();
            BeanUtils.copyProperties(sendCmdDTO, cmdCallBackDTO);
            cmdCallBackDTO.setStatus(true);
            rabbitTemplate.convertAndSend("cmdCallBack", null, cmdCallBackDTO);
            removeCmd(sendCmdDTO);
        }
    }

    public void sendRealTimeData(List<RealTimeDataDTO> dataResult) {
        rabbitTemplate.convertAndSend(REAL_TIME_DATA_FANOUT_EXCHANGE, null, dataResult);
    }

    public void sendDeviceStatus(DeviceStatusDTO deviceStatusDTO) {
        rabbitTemplate.convertAndSend(FANOUT_EXCHANGE_DEVICE_STATUS, null, deviceStatusDTO);
    }
    public void sendDeviceStatusOnline(Integer deviceId) {
        log.info("send device onLine deviceId:{}",deviceId);
        DeviceStatusDTO deviceStatusDTO = new DeviceStatusDTO(deviceId, DeviceStatusEnum.ONLINE.getCode());
        sendDeviceStatus(deviceStatusDTO);
    }
    public void sendDeviceStatusOffline(Integer deviceId) {
        log.info("send device offLine deviceId:{}",deviceId);
        DeviceStatusDTO deviceStatusDTO = new DeviceStatusDTO(deviceId, DeviceStatusEnum.OFFLINE.getCode());
        sendDeviceStatus(deviceStatusDTO);
    }



    public void sendPolice(List<AlarmEvent> polices) {
        rabbitTemplate.convertAndSend(POLICE_FANOUT_EXCHANGE, null, polices);
    }

    public void sendRestore(List<AlarmEvent> polices) {
        rabbitTemplate.convertAndSend(RESTORE_FANOUT_EXCHANGE, null, polices);
    }
    public void sendCmd(CommandDTO commandDTO) {
//        rabbitTemplate.convertAndSend(COMMAND, BaseHandler.RADAR, radarCommandDTO);
    }

    private void removeCmd(SendCmdDTO v) {
//        String key = v.getKey();
//        BaseHandler.sendCmdMap.remove(key);
    }
}
